package com.mycompany.fragment;

import android.util.Log;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by rzw4v0 on 2015/10/22.
 */
public class ReadFile {

    public static byte[] getFileFromUrl(String url, NetWorkSpeedInfo netWorkSpeedInfo) {
        int currentByte = 0;
        int fileLength = 0;
        long startTime = 0;
        long intervalTime = 0;

        byte[] b = null;

        int bytecount = 0;
        URL urlx = null;
        URLConnection con = null;
        InputStream stream = null;
        try {
            Log.d("URL:", url);
            urlx = new URL(url);
            con = urlx.openConnection();
            con.setConnectTimeout(20000);
            con.setReadTimeout(20000);
            fileLength = con.getContentLength();
            stream = con.getInputStream();
            netWorkSpeedInfo.totalBytes = fileLength;
            b = new byte[fileLength];
            startTime = System.currentTimeMillis();
            while ((currentByte = stream.read()) != -1) {
                netWorkSpeedInfo.hadFinishedBytes++;
                intervalTime = System.currentTimeMillis() - startTime;
                if (intervalTime == 0) {
                    netWorkSpeedInfo.speed = 1000;
                } else {
                    netWorkSpeedInfo.speed = (netWorkSpeedInfo.hadFinishedBytes / intervalTime) * 1000;
                }
                if (bytecount < fileLength) {
                    b[bytecount++] = (byte) currentByte;
                }
            }
        } catch (Exception e) {
            Log.e("exception : ", e.getMessage() + "");
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (Exception e) {
                Log.e("exception : ", e.getMessage());
            }
        }
        return b;
    }

}
